<?php

/*
 * group at_info 
 * @author yangchao
 * @package group
 * @copyright yangchao
 * @version 1.2
 * @code-encode utf-8
 * @data-encode utf-8
*/

if (isset($_GET["test_db"]))
{
	include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
}
define("CACHE_PREFIX_user_feed","_user_feed_");
class db_user_feed extends db_base
{
	var $tbl_name = "user_feed";

	function db_user_feed()
	{
		parent::db_base();
	}

//前台程序开始
	function get_user_feed_list_by_uids($uids,$p=1,$pcount=20)
	{       
		if(is_array($uids))
		{
			$uids = join(",",$uids);
		}
		$uids = mysql_escape_string($uids);
		
		$data = false;
		$key = CACHE_PREFIX_user_feed. "-get_user_feed_list_by_uids-{$uids}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " uid in ({$uids}) and  status = 1 " ;	
			$data = $this->get_alllist($this->tbl_name,$p,$pcount ,"id desc" ,$where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}
	
	
	
	function get_user_feed_count_by_uids($uids)
	{       
		if(is_array($uids))
		{
			$uids= array_unique($uids);
			$uids = join(",",$uids);
		}
		$uids = mysql_escape_string($uids);
		
		$data = false;
		$key = CACHE_PREFIX_user_feed. "-get_user_feed_count_by_uids-{$uids}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " uid in ({$uids}) and  status = 1 " ;	
			$data = $this->get_listcount($this->tbl_name,$where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}	
	
	
	function get_user_feed_unread_count_by_uids($uids,$id)
	{       
		if(is_array($uids))
		{
			$uids= array_unique($uids);
			$uids = join(",",$uids);
		}
		$uids = mysql_escape_string($uids);
		
		//处理where条件
		$where = " uid in ({$uids}) and id> $id and  status = 1 " ;	
		$data = $this->get_listcount($this->tbl_name,$where);
		$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
	

		return $data;
	}	
	
	
	function get_feed_by_uid_and_type_today($uid,$type,$p=1,$pcount=10)
	{
		$today = date("Y-m-d");
		$where = " uid =$uid and create_date= '{$today}' and data_type = $type and   status = 1 " ;	
		
		return $this->get_alllist($this->tbl_name,$p,$pcount,"",$where);
	}
		
	//添加一条数据
	//$data_type 1 喜欢mblog 2 发分享 3 加入专辑
	
	function insert_user_feed(
		$uid,
		$data_detail,
		$data_type
		
	)
	{
		global $config;
		//详细字段
		$ary = array();
		$ary["uid"] = $uid;
		$ary["data_detail"] = $data_detail;
		$ary["data_type"] = $data_type;
		
		
		$ary["create_date"] = Date::get_date();
		$ary["update_time"] = Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		$ary["status"] = 1;
		
		
		$ret = $this->insert($this->tbl_name, $ary);
		if ($ret)
		{
			
		}
		else
		{
			return false;
		}

		return $this->get_lastinsertid();
	}

	
	function update_user_feed_by_id($id,$data_detail,$status="")
	{
		
		$where = "id = $id";
		
		if(strlen($data_detail)>0)$ary["data_detail"] = $data_detail;
		if(strlen($status)>0)$ary["status"] = $status;
		
		$ary["update_time"] = Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		
		
		return $this->update($this->tbl_name,$ary,$where);
	}
	
	

}


